/*
 *    Copyright 2022 jacky4631 shen ji email:121659050@qq.com
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.dataoke.shoppingguide.app773026.activity.home.fragment;


import static android.graphics.Typeface.BOLD;
import static android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE;
import static com.dataoke.shoppingguide.app773026.bean.Constants.PRODUCT_ID_KEY;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.dataoke.shoppingguide.app773026.R;
import com.dataoke.shoppingguide.app773026.activity.detail.DataokeProductDetailActivity;
import com.dataoke.shoppingguide.app773026.bean.ParseContent;
import com.dataoke.shoppingguide.app773026.util.GlideUtils;

/**
 * 查券弹窗
 * @author: shen ji
 * @date: 2022/3/15
 * @email: 121659050@qq.com
 * @wechat: jacky4631
 */
public class ParseTaoWordDialogFragment extends DialogFragment implements DialogInterface.OnCancelListener, DialogInterface.OnDismissListener {
    private TextView mTvTitle;
    private TextView mTvPrice;

    private ImageView mIvPic;
    private Button mBtnDetail;
    private Button mBtnClose;
    private ImageView mIvClose;
    private ParseContent parseContent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.parseContent = (ParseContent) getArguments().getSerializable("content");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_popup, container, false);
        initView(view);
        return view;
    }

    private void initView(View dialog) {
        mIvPic = dialog.findViewById(R.id.iv_pic);
        mBtnDetail = dialog.findViewById(R.id.btn_detail);
        mBtnClose = dialog.findViewById(R.id.btn_close);
        mTvTitle = dialog.findViewById(R.id.tv_title);
        mTvPrice = dialog.findViewById(R.id.tv_price);
        mIvClose = dialog.findViewById(R.id.iv_close);


        GlideUtils.loadImgPopupFit(getContext(), parseContent.getMainPic(), mIvPic);
        mTvTitle.setText(parseContent.getOriginInfo().getTitle());
        initEndPrice();
        mBtnDetail.setOnClickListener(v -> {

            Intent intent = new Intent(getContext(), DataokeProductDetailActivity.class);
            intent.putExtra(PRODUCT_ID_KEY, parseContent.getGoodsId());
            getContext().startActivity(intent);
            dismiss();
        });
        mBtnClose.setOnClickListener(v -> dismiss());
        mIvClose.setOnClickListener(v -> dismiss());

    }
    private void initEndPrice() {
        double price = Double.parseDouble(parseContent.getOriginInfo().getPrice());
        double amount = Double.parseDouble(parseContent.getOriginInfo().getAmount());
        double endPrice = price - amount;

        String priceTxt = "￥" + endPrice + " ￥" + price;
        SpannableStringBuilder builder = new SpannableStringBuilder(priceTxt);

        //ForegroundColorSpan 为文字前景色，BackgroundColorSpan为文字背景色
        ForegroundColorSpan graySpan = new ForegroundColorSpan(Color.GRAY);
        StrikethroughSpan stSpan = new StrikethroughSpan();  //设置删除线样式
        StyleSpan styleSpan = new StyleSpan(BOLD);


        int index = priceTxt.indexOf(" ");

        builder.setSpan(new AbsoluteSizeSpan(15, true), 0, index, SPAN_EXCLUSIVE_EXCLUSIVE);
        builder.setSpan(styleSpan, 0, index, SPAN_EXCLUSIVE_EXCLUSIVE);
        builder.setSpan(stSpan, index + 1, priceTxt.length(), SPAN_EXCLUSIVE_EXCLUSIVE);
        builder.setSpan(graySpan, index + 1, priceTxt.length(), SPAN_EXCLUSIVE_EXCLUSIVE);
        mTvPrice.setText(builder);

    }
}
